package com.easyrecite.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDao {
    private Connection connection = null;
    private Statement statement;
    private ResultSet resultSet;

    public void userLogin() throws SQLException {
        String sql = "select * from tb_user where user_name = \'jack\' and user_pwd = \'123456\'";
        connection = MySQLHelper.getConnection();
        statement = connection.createStatement();
        resultSet = statement.executeQuery(sql);
        while (resultSet.next()){
            String userName = resultSet.getString(2);
            String pwd = resultSet.getString(3);
            System.out.println(userName+" "+pwd);
        }
        resultSet.close();
        statement.close();
    }

    public void signUp(String username, String phone, String password) throws SQLException {
        String sql = "insert into tb_user (user_name, user_phone, user_pwd) values('" + username + "', '" + phone + "', '" + password + "')";
        connection = MySQLHelper.getConnection();
        statement = connection.createStatement();
        statement.execute(sql);
    }

    public boolean login(String phone, String password) throws SQLException {
        String sql = "select * from tb_user where user_phone = '"+phone+"' and user_pwd = '"+password+"'";        connection = MySQLHelper.getConnection();
        statement = connection.createStatement();
        resultSet = statement.executeQuery(sql);
        return resultSet.next();
    }
}
